package zhoufu.sz.sanxiao_check.ui.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;


/**
 * Created by nuuneoi on 11/16/2014.
 */
public class StatedFragment extends Fragment
{

    Bundle savedState;

    public StatedFragment()
    {
        super();
        if ( getArguments() == null )
        {
            setArguments(new Bundle());
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        // Restore State Here
        if ( !restoreStateFromArguments() )
        {
            // First Time, Initialize something here
            onFirstTimeLaunched();
        }
    }

    /**
     * Called when the fragment is launched for the first time. In the other
     * words, fragment is now recreated.
     */

    protected void onFirstTimeLaunched()
    {

    }

    @Override
    public void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);
        // Save State Here
        saveStateToArguments();
    }

    @Override
    public void onDestroyView()
    {
        super.onDestroyView();
        // Save State Here
        saveStateToArguments();
    }

    // //////////////////
    // Don't Touch !!
    // //////////////////

    private void saveStateToArguments()
    {
        if ( getView() != null )
        {
            savedState = saveState();
        }
        if ( savedState != null )
        {
            Bundle b = getArguments();
            if ( b != null )
            {
                b.putBundle("internalSavedViewState8954201239547", savedState);
            }
        }
    }

    // //////////////////
    // Don't Touch !!
    // //////////////////

    private boolean restoreStateFromArguments()
    {
        Bundle b = getArguments();
        if ( b != null )
        {
            savedState = b.getBundle("internalSavedViewState8954201239547");
            if ( savedState != null )
            {
                restoreState();
                return true;
            }
        }
        return false;
    }

    // ///////////////////////////////
    // Restore Instance State Here
    // ///////////////////////////////

    private void restoreState()
    {
        if ( savedState != null )
        {
            // For Example
            // tv1.setText(savedState.getString("text"));
            onRestoreState(savedState);
        }
    }

    /**
     * Called when the fragment's activity has been created and this fragment's
     * view hierarchy instantiated. It can be used to do final initialization
     * once these pieces are in place, such as retrieving views or restoring
     * state. This is called after {@link #onCreateView} and before
     * {@link #onViewStateRestored(Bundle)}.
     *
     * @param savedInstanceState If the fragment is being re-created from a previous saved
     * state, this is the state.
     */

    protected void onRestoreState(Bundle savedInstanceState)
    {

    }

    // ////////////////////////////
    // Save Instance State Here
    // ////////////////////////////

    private Bundle saveState()
    {
        Bundle state = new Bundle();
        // For Example
        // state.putString("text", tv1.getText().toString());
        onSaveState(state);
        return state;
    }

    /**
     * Called to ask the fragment to save its current dynamic state, so it can
     * later be reconstructed in a new instance of its process is restarted. If
     * a new instance of the fragment later needs to be created, the data you
     * place in the Bundle here will be available in the Bundle given to
     * {@link #onRestoreState(Bundle)}.
     * <p/>
     * <p/>
     * This corresponds to {@link Activity#onSaveInstanceState(Bundle)
     * Activity.onSaveInstanceState(Bundle)} and most of the discussion there
     * applies here as well. Note however: <em>this method may be called
     * at any time before {@link #onDestroy()}</em>. There are many situations
     * where a fragment may be mostly torn down (such as when placed on the back
     * stack with no UI showing), but its state will not be saved until its
     * owning activity actually needs to save its state.
     *
     * @param outState Bundle in which to place your saved state.
     */

    protected void onSaveState(Bundle outState)
    {

    }

    //    @Override
    //    public void onStart() {
    //        super.onStart();
    //        ActivityResultBus.getInstance().register(mActivityResultSubscriber);
    //    }
    //
    //    @Override
    //    public void onStop() {
    //        super.onStop();
    //        ActivityResultBus.getInstance().unregister(mActivityResultSubscriber);
    //    }

    //    private Object mActivityResultSubscriber = new Object() {
    //        @Subscribe
    //        public void onActivityResultReceived(ActivityResultEvent event) {
    //            int requestCode = event.getRequestCode();
    //            int resultCode = event.getResultCode();
    //            Intent data = event.getData();
    //            onActivityResult(requestCode, resultCode, data);
    //        }
    //    };
}
